package com.csu.csumall.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class AlipayUtil {

    private final String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    private final String appId = "2016102600764271";
    private final String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCTQDElhCtM7K4dLXLJN/OVI3GmU/YBIVa5z80YN4tGlBJUQAEQswOjvLNUlQhV3xq1G1oFXNlN/eGnMIRlXoxZx3orPO+SDffwzs0BjIMoBRrHsQZRVj+d0bYIwa9xIGxTQNQiyzVEzsczHmFjmH1GZ/SdCLCJrl2rvHCVx8jpheDc6IYTHswK4BSWIee0PBPtUHnciX/d2ja3Sidox5ACfBNO6SdhvpaVni2fhwVCToUg+xkI5ZpLDVTQaMazO4ZhXy8WSRJEVCKNfEg4QDSYscxToMltOoDwHtp7zLuQaFEtI8Lb/otxUk9XD9rq689PbdWLTNEdXhic5hSM3fjXAgMBAAECggEAFDeOB96bq1YbmbnxtprOAL37DqEcd49CPwS+3gJSRwBxe8vF2asaZtKztHs5x8B4BEGbHH1hAIpI/xF0PA69Kl5u8ZzOL7lnP1LFZt8J6ua4hI5TlbtHxL1URuOA2QFgnhWaiqRnzeiuBOC48lmvaYlAVoco5d5g0gFvW8EwXo9jiJ8lkQm7gm9XTsfIAvvtfFIGtDx+9ynlO/8B5vjlaBmUxzp3akj6/2EUGy+HvBt6Ps49OEPhAEH7oZ7SnV5pl1IQh0oqBuyFWSkh67F3XX+kLK4SuMwiGT3/ipTohkANegysj1fEV9WhrSrkQqykDPVhUzdoG9d8dOhrrJV8oQKBgQDUDJjeeuaYLQLvwK1FEkwmMpf88qXFR1OpXWJSLd/d6rm4dC9eWRJ6C7I3NFq4VHcPcP0O/w0NJ2Y9UuItDm4Tbm7ssrI68Pe2b2m/ybFnT7ZeiWHqvokXrBH7aAzijiTO4MX2c7dHM/+M2JxfEcNYCPuGgM6iJbg4ALlsLSqDSQKBgQCxxV3f6/+gfKbd/OoOuUTBZWHayy98zXfNpyvGuIVb6mFtNedJx9mnMpNhq6cf7iYH3AU1RO9yVbKz7IcbCEtJs/VT7t1N/cP5spJcI5Q7/O7c6NoAHaBt1fFsLSvt29a4lFexUlyrpBaSY7D0JoS9VRYexYHmuUufgOTGRoR7HwKBgQCx78gvmQ37UHoQWayMM2h2/SmQ9sSoIMxRaK1irGAYxikGYWiGUVQx2bK4pFQkLayi5ntTpC59hTxD5VMp39v6rkailOj+kMos6pzgdQ1K1hcAiWQDKdO8a4XDY3K8ge7dMryrrDp1o2qj8/hzxmIXc6ItIdVDN0e4ptLxtYzhEQKBgQCdRQluv39QANczTrvWuMr9+W4fvV/OnovdkYZl8PpyPaFhl619lWOVymWeo9hfpDVbAKhO3ySi1vQYvykrniPVGXLxhUGd7OICtptLDmLmyzaakxnBvnQel4ZU+IWDkdidRQdrpXy8kJczfSSgaq9GPM8r+/mXH9iqTMKYMuyjawKBgDHgStiIJDsBNPPSHlsF8Wg3p2pgZqwoajAYdbOS20uSx2vTPDB074Fc6Aldf8GbT5kW9JBxY2bwv6adp3rLAsfZYr07grOkD3jNMIA+o1/5XO8rkDud8bWFLAsxD85g+oxSzD3SuIrAiAi7NELBJD1aetcs27S7Q1lTVJBXFJYB";
    private final String format = "json";
    private final String charset = "utf-8";
    private final String alipayPulicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAknCwiPXEnmz7zBUbIn2TYSaLzirCS7IKBJ1jwVe0NBN7UPdQVWREH4V74klPiBuXMPU6yAoS10pFOBsjYfyyusTK0eShRBsoiGTl2qbye9aXFrOnlaUnBLyjI+s/6jHNkgrf/n+nfTFRDkGNFVd9TCBcmyfbL7dBW0pG34KNf3WJ++xiJk5TYLV6jWf1iONMCv+XO8IYbMrCdN3RiSgRbxtH7DC71XNYoTiltJ1BNSFUcJxDk09kWFOdyvQFtWhJWxVYYmfHfuImfwkcksrnpSlHSMWRjTeHqWelgFgnahfjme5Xd/9GP+CDOb1xVEJrOxEhkuuAMXh2XfW0kworSQIDAQAB";
    private  final String signType = "RSA2";

    public AlipayUtil() {
//        log.info("构造函数");
    }

    public String preCreate () {
        AlipayClient alipayClient =  new DefaultAlipayClient(
                serverUrl, appId, privateKey, format, charset, alipayPulicKey, signType);
        AlipayTradePrecreateRequest request   =   new AlipayTradePrecreateRequest();
        request . setBizContent ( "{"   +
                "    \"out_trade_no\":\"20200710010101010\","   + //商户订单号
                "    \"total_amount\":\"88.88\","   +
                "    \"subject\":\"Iphone6 16G\","   +
                "    \"store_id\":\"NJ_001\","   +
                "    \"timeout_express\":\"90m\"}" ); //订单允许的最晚付款时间
        AlipayTradePrecreateResponse response  = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.print(response.getBody());
        return response.getBody();
    }

    public AlipayTradePrecreateResponse preCreate (String outTradeNo, String totalAmount, String subject) {
        AlipayClient alipayClient =  new DefaultAlipayClient(
                serverUrl, appId, privateKey, format, charset, alipayPulicKey, signType);
        AlipayTradePrecreateRequest request   =   new AlipayTradePrecreateRequest();
        request.setBizContent("{"  +
                "\"out_trade_no\":\"" + outTradeNo + "\"," + //商户订单号
                "\"total_amount\":\"" + totalAmount + "\"," +
                "\"subject\":\"" + subject + "\"," +
                "\"store_id\":\"CSU_001\"," +
                "\"timeout_express\":\"90m\"}"); //订单允许的最晚付款时间
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.print(response.getBody());
        return response;
    }

    public String query (String outTradeNo) {
        AlipayClient alipayClient =  new DefaultAlipayClient(
                serverUrl, appId, privateKey, format, charset, alipayPulicKey, signType);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); //创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + outTradeNo + "\""   +
                "}");
//                "\"trade_no\":\"2014112611001004680073956707\"}");  //设置业务参数
        AlipayTradeQueryResponse response = null; //通过alipayClient调用API，获得对应的response类
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e){
            e.printStackTrace();
        }
        System.out.print(response.getSubCode());
        //根据response中的结果继续业务逻辑处理
        return response.getBody();
    }

    public String query () {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipay.com/gateway.do", appId, privateKey, "json", charset, alipayPulicKey, "RSA2");  //获得初始化的AlipayClient
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); //创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\"20150320010101001\","   +
                "\"trade_no\":\"2014112611001004680073956707\"}");  //设置业务参数
        AlipayTradeQueryResponse response = null; //通过alipayClient调用API，获得对应的response类
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e){
            e.printStackTrace();
        }
        System.out.print(response.getBody());
        //根据response中的结果继续业务逻辑处理
        return response.getBody();
    }

}
